package com.swinginwind.flexutils.components.timeComp.time
{
	/**
	 * 描述: 时间输入组件
	 * @版权所有: horizo
	 * @创建者: caoww
	 * @创建日期: 2011-1-10 
	 * @修改者: 
	 * @修改日期: 
	 * @修改说明: 
	 */
	import com.swinginwind.flexutils.components.timeComp.common.helper.*;
	
	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.events.KeyboardEvent;
	
	import mx.binding.utils.ChangeWatcher;
	import mx.events.FlexEvent;
	
	import spark.components.BorderContainer;
	import spark.components.Group;
	import spark.components.TextInput;
	
	public class TimeTextInput extends BorderContainer
	{
		public function TimeTextInput()
		{
			super();
			this.height=25;
		}
		
		public override function get baselinePosition():Number
		{
			var textinput:TextInput=new TextInput()
			return getBaselinePositionForPart(textinput);
		}
		
		
		private var _selectedDate:Date;
		
		/**选定的时间*/
		public function get selectedDate():Date
		{
			return _selectedDate;
		}
		[Bindable("change")]
		[Bindable("dataChange")]
		/**
		 * @private
		 */
		public function set selectedDate(value:Date):void
		{
			if(_selectedDate!=value){
				_selectedDate = value;
				dispatchEvent(new Event("dataChange"));
			}
		}
		
		
		private var _selectedDataStr:String;
		
		/**
		 * 选定的时间的字符串格式:YYYY/MM/DD JJ:NN:SS;<br/>
		 * 优先级高于属性selectedDate
		 */
		public function get selectedDataStr():String
		{
			return _selectedDataStr;
		}
		[Bindable("change")]
		[Bindable("textChanged")]
		/**
		 * @private
		 */
		public function set selectedDataStr(value:String):void
		{
			_selectedDataStr = value;
			dispatchEvent(new Event("textChanged"));
		}
		
		/**是否默认显示当前时间*/
		public var defaultTime:Boolean=false;
		/**刷新当前时间*/
		private function refreshTime():void{
			var YYYY:uint=textInputValue(0)*1000+textInputValue(1)*100+textInputValue(2)*10+textInputValue(3);
			var MM:uint=textInputValue(4)*10+textInputValue(5);
			var DD:uint=textInputValue(6)*10+textInputValue(7);
			var JJ:uint=0;
			var NN:uint=0;
			var SS:uint=0;
			switch(formatString){
				case "YYYY/MM/DD JJ:NN:SS":
					SS=textInputValue(12)*10+textInputValue(13)
				case "YYYY/MM/DD JJ:NN":
					JJ=textInputValue(8)*10+textInputValue(9);
					NN=textInputValue(10)*10+textInputValue(11);
					break;
				case "YYYY/MM/DD":				
					//none
					break;
			}
			MM--;//0表示1月
			if(YYYY>0){
				selectedDate=new Date(YYYY,MM,DD,JJ,NN,SS);
				selectedDataStr=DateHelper.dateToString(selectedDate,formatString);		
			}			
		}
		
		[Inspectable(category="General", enumeration="YYYY/MM/DD JJ:NN:SS,YYYY/MM/DD JJ:NN,YYYY/MM/DD", defaultValue="YYYY/MM/DD JJ:NN:SS")]
		/**时间显示格式*/
		public var formatString:String="YYYY/MM/DD JJ:NN:SS";
		
		private var timeGroup:Group;
		
		override protected function createChildren():void{
			timeGroup=new Group();
			var group:Group=new Group();
			var datastr:String="0000/00/00 00:00:00";
			if(defaultTime){
				datastr=DateHelper.dateToString(new Date(),formatString);
			}else{
				switch(formatString){
					case "YYYY/MM/DD JJ:NN:SS":
						datastr="0000/00/00 00:00:00";
						this.width=167;
						break
					case "YYYY/MM/DD JJ:NN":
						datastr="0000/00/00 00:00";
						this.width=143;
						break;
					case "YYYY/MM/DD":
						datastr="0000/00/00";
						this.width=95;
						break;
				}
			}
			for(var i:uint=0;i<datastr.length;i++){
				var text:String=datastr.substr(i,1);
				var textinput:TextInput;
				
				if(StringHelper.isNumeric(text)){
					textinput=getTextInput(text,timeGroup.numElements);
					textinput.x=8*i;
					timeGroup.addElement(textinput);	
				}else{
					textinput=getTextInput(text,i);
					textinput.x=8*i;
					group.addElement(textinput);
				}
			}
			group.left=5;
			group.right=5;
			group.top=2;
			group.bottom=0;			
			group.enabled=false;
			this.addElement(group);
			timeGroup.left=5;
			timeGroup.right=5;
			timeGroup.top=2;
			timeGroup.bottom=0;
			this.addElement(timeGroup);
			super.createChildren();
			this.addEventListener(FocusEvent.FOCUS_OUT,function():void{
				refreshTime();
			});
			this.addEventListener(FlexEvent.CREATION_COMPLETE,function():void{
				ChangeWatcher.watch(this,"selectedDate",dataChangeFun);
			});
		}
		
		private function dataChangeFun(e:Event):void{
			if(selectedDate){
				var dateStr:String=DateHelper.dateToString(selectedDate,formatString);
				var index:uint=0;
				for(var i:uint=0;i<dateStr.length;i++){
					var text:String=dateStr.substr(i,1);
					var textinput:TextInput;					
					if(StringHelper.isNumeric(text)){
						textInputItem(index).text=text;
						index++;
					}
				}
			}
		}
		//生成textinput
		private function getTextInput(text:String,index:uint):TextInput{
			var textinput:TextInput=new TextInput();
			textinput.name=String(index);
			textinput.text=text;
			textinput.restrict="0-9";
			textinput.maxChars=1;
			textinput.width=8;
			textinput.setStyle("borderVisible","false");
			textinput.setStyle("focusAlpha","0");
			textinput.setStyle("textAlign","center");
			switch(name){
				case "/":
				case " ":
				case ":":
					textinput.editable=false;					
					textinput.setStyle("focusEnabled","false");
					break;
				default:
					textinput.addEventListener(KeyboardEvent.KEY_UP,textinputKeyDown);
					break;
			}
			return textinput;
		}
		/**判断焦点是向后移还是前移*/
		private var focusNext:Boolean=true;
		private function textValueCheck(textinput:TextInput):void{	
			var num:uint=uint(textinput.text);
			var MM:uint=textInputValue(4)*10+textInputValue(5);//月份
			switch(textinput.name){
				case "4":
					if(num>1){
						nowValueToNext(uint(textinput.name));
					}else if(num==1){
						if(textInputValue(5)>2)
							textInputItem(5).text="0";
					}
					break;
				case "5":
					if(textInputValue(4)==1){
						if(num>2)
							textinput.text="2";
					}
					break;
				case "6":					
					switch(MM){
						case 1:
						case 3:
						case 5:
						case 7:
						case 8:
						case 10:
						case 12:
							//31天
							if(num>3){
								nowValueToNext(uint(textinput.name));
							}else if(num==3){
								if(textInputValue(7)>0)
									textInputItem(5).text="0";
							}
							break;
						case 4:
						case 6:
						case 9:
						case 11:
							//30天	
							if(num>3){
								nowValueToNext(uint(textinput.name));
							}else if(num==3){
								if(textInputValue(7)>1)
									textInputItem(5).text="0";
							}
							break;
						case 2:
							//28天
							if(num>2){
								nowValueToNext(uint(textinput.name));
							}else if(num==2){
								if(textInputValue(7)>8)
									textInputItem(5).text="0";
							}
							break;							
					}					
					break;
				case "7":
					switch(MM){
						case 1:
						case 3:
						case 5:
						case 7:
						case 8:
						case 10:
						case 12:
							//31天
							if(textInputValue(6)==3){
								if(num>1)
									textinput.text="1";	
							}
							break;
						case 4:
						case 6:
						case 9:
						case 11:
							//30天	
							if(textInputValue(6)==3){
								if(num>0)
									textinput.text="0";	
							}
							break;
						case 2:
							//28天
							if(textInputValue(6)==2){
								if(num>8)
									textinput.text="8";	
							}
							break;							
					}	
					break;
				case "8":
					if(num>2){
						nowValueToNext(uint(textinput.name));
					}
					break;
				case "9":
					if(textInputValue(10)==2){
						if(num>3)
							textinput.text="3";	
					}
					break;
				case "10":
				case "12":
					//分钟、秒钟的第一位
					if(num>5){
						nowValueToNext(uint(textinput.name));
					}
					break;
				case "11":
				case "13":
					//分钟、秒钟的第二位
					//none
					break;
				
			}			
			moveTextInputFocus(uint(textinput.name));
		}
		
		private function nowValueToNext(index:uint):void{
			textInputItem(index+1).text=textInputItem(index).text;
			textInputItem(index).text="0";
			moveFocusTwo=true;
		}
		private function textInputValue(index:uint):uint{
			var value:uint;
			if(index>=0&&index<timeGroup.numElements){
				var textinput:TextInput=timeGroup.getElementAt(index) as TextInput;
				value=uint(textinput.text);
			}
			return value;
		}
		private function textInputItem(index:uint):TextInput{
			var textinput:TextInput;
			if(index>=0&&index<timeGroup.numElements){
				textinput=timeGroup.getElementAt(index) as TextInput;
			}
			return textinput;
		}
		
		
		//处理按键
		private function textinputKeyDown(e:KeyboardEvent):void{
			e.stopPropagation();
			var keycode:uint=e.keyCode;
			var textinput:TextInput=e.currentTarget as TextInput;
			if(keycode>=48&&keycode<=57){
				//处理输入数字
				focusNext=true;
				textinput.text=String.fromCharCode(e.charCode);	
				textValueCheck(textinput)				
			}else if(keycode==8){
				//删除
				textinput.text="0";
				focusNext=false;
				moveTextInputFocus(uint(textinput.name));
			}else if(keycode==37){
				focusNext=false;
				moveTextInputFocus(uint(textinput.name));
			}else if(keycode==39){
				focusNext=true;
				moveTextInputFocus(uint(textinput.name));
			}
			
		}
		private var moveFocusTwo:Boolean=false;
		//移动焦点
		private function moveTextInputFocus(index:uint):void{
			if(focusNext){
				index++;
			}else
				index--;
			if(index>=0&&index<timeGroup.numElements){
				var textinput:TextInput=timeGroup.getElementAt(index) as TextInput;
				textinput.setFocus();
			}
			if(moveFocusTwo){
				moveFocusTwo=false;
				moveTextInputFocus(index);
				
			}
		}
	}
}